<%-- 
    Document   : products
    Created on : May 17, 2013, 1:30:45 PM
    Author     : Kate
--%>
<%@page import="entity.Category"%>
<%@page import="entity.Product"%>
<%@page import="java.util.List"%>
<%@page import="javax.naming.NamingException"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="facade.CategoryFacadeLocal"%>
<%@page import="facade.ProductFacadeLocal"%>
<%! private ProductFacadeLocal productFacade;
    private CategoryFacadeLocal categoryFacade;

    public void jspInit() {
        try {
            InitialContext ic = new InitialContext();
            productFacade = (ProductFacadeLocal) ic.lookup(
                    "java:global/Delta/Delta-ejb/ProductFacade!facade.ProductFacadeLocal");
            categoryFacade = (CategoryFacadeLocal) ic.lookup(
                    "java:global/Delta/Delta-ejb/CategoryFacade!facade.CategoryFacadeLocal");

        } catch (NamingException ex) {
            ex.printStackTrace();
        }
    }
%>
<%
    int pageNumber;
    String pageNum;
    int maxResults;
    int firstResult;
    String id_category_p;
    int id_category;
    List<Product> products = null;
    String productName;
    String sortBy;
    int sortField;
    boolean isAsc;
    Category category;
    long count = -1;
%>

<%

    //parse pageNumber
    pageNum = request.getParameter("pageNumber");
    try {
        pageNumber = Integer.parseInt(pageNum);
    } catch (NumberFormatException ex) {
        pageNumber = 1;
    }
    maxResults = 10;
    firstResult = maxResults * (pageNumber - 1);

    //parse category
    id_category_p = request.getParameter("category");
    try {
        id_category = Integer.parseInt(id_category_p);
    } catch (NumberFormatException ex) {
        id_category = -1;
    }

    //parse name
    productName = request.getParameter("name");
    if (productName == null) {
        productName = "";
    }

    //parse sortBy
    String sortBy_p = request.getParameter("sortBy");
    try {
        sortField = Integer.parseInt(sortBy_p);
    } catch (NumberFormatException ex) {
        sortField = 0;
    }

    switch (sortField) {
        case 1:
            sortBy = "price";
            isAsc = true;
            break;
        case 2:
            sortBy = "price";
            isAsc = false;
            break;
        case 3:
            sortBy = "name";
            isAsc = false;
            break;
        default:
            sortBy = "name";
            isAsc = true;
            break;
    }

%>

<%
    System.out.println(productName);
    System.out.println(id_category);


    if (productName != "" && id_category != -1) {
        category = categoryFacade.find(id_category);
        products = productFacade.findAll(firstResult, maxResults, sortBy, isAsc, productName, category);
        count = productFacade.countAllByNameCategory(productName, categoryFacade.find(id_category));
    }
    if (productName != "" && id_category == -1) {
        products = productFacade.findAll(firstResult, maxResults, sortBy, isAsc, productName);
        count = productFacade.countAllByName(productName);
    }
    if (productName == "" && id_category != -1) {
        category = categoryFacade.find(id_category);
        products = productFacade.findAll(firstResult, maxResults, sortBy, isAsc, category);
        count = productFacade.countAllByCategory(category);

    }
    if (productName == "" && id_category == -1) {
        products = productFacade.findAll(firstResult, maxResults, sortBy, isAsc);
        count = productFacade.countAll();
    }

    
%>

      <table>
                        <TR>
                            <td id="head_table_zero"></td>
                            <td id="head_table_first"><strong>Article</strong></td>
                            <td id="head_table_first"><strong>Product Name</strong></td>
                            <td id="head_table_second"><strong>Category Name</strong></td>
                            <td id="head_table_second"><strong>Price</strong></td>
                            <td id="head_table_second"><strong>Status</strong></td>
                            <td id="head_table_third"><strong>Description</strong></td>
                        </TR>

                        <%
                          
                    
                            for (Product p : products) {%>
                        <TR>
                            <td> 
                                <input type="radio" name="id_product" value=<%=p.getId_product()%>>
                            </td>
                            <td id="colomn_first" ><%=p.getId_product()%> </td>
                            <td id="colomn_first" ><div><%=p.getName()%> </div> </td>
                            <td id="colomn_first" ><%=p.getCategory().getName()%> </td>
                            <td id="colomn_second"><%=p.getPrice()%> </td>
                            <td id="colomn_second"><span id="productStatus_<%=p.getId_product()%>"><%=p.getStatus() == 0 ? "active" : "discontinued"%></span> </td>
                            <td id="colomn_third"><div> <%=p.getDescription()%> </div> </td>
                        </TR> 
                        <%}%>

                    </table>
                        <%
                        if (products.isEmpty()) {
        out.print("<center><font color=\"red\"><b>Sorry, no products found on your search<b></font></center>");
    }%>
                        <input
    id="count"
    name="count"
    type="hidden"
    value="<%=count%>"/>

